package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.R;
import com.itheima.dao.AddressBookDao;
import com.itheima.pojo.AddressBook;
import com.itheima.service.AddressBookService;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.http.HTTPBinding;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;

    /**
     * 新增地址
     */
    @PostMapping
    public R addressBook(@RequestBody AddressBook addressBook, HttpServletRequest request){
        //获取用户id
        Long userId = (Long) request.getSession().getAttribute("user");
        log.info(String.valueOf(userId));
        addressBook.setUserId(userId);
        addressBook.setCreateUser(userId);
        addressBook.setUpdateUser(userId);
        addressBookService.save(addressBook);
        return R.success("保存地址成功");
    }


    /**
     * 设置默认地址
     * is_default值：1或0，1为默认地址
     */

    @PutMapping("/default")
    public R defaultAddress(@RequestBody AddressBook addressBook,HttpServletRequest request){
        //获取session中的userId
        Long userId = (Long) request.getSession().getAttribute("user");
        //将该用户所有的地址的is_default的值修改为0
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(AddressBook::getIsDefault,0).eq(AddressBook::getUserId,userId);
        addressBookService.update(wrapper);
        //将设置为默认地址is_default值设置为1
        LambdaUpdateWrapper<AddressBook> wrapper2 = new LambdaUpdateWrapper<>();
        wrapper2.set(AddressBook::getIsDefault,1).eq(AddressBook::getId,addressBook.getId());
        addressBookService.update(wrapper2);
        return R.success("设置成功");
    }

    /**
     * 根据ID查询地址
     */

    @GetMapping("/{id}")
    public R selectById(@PathVariable Long id){
        AddressBook address = addressBookService.getById(id);
        return R.success(address);
    }

    /**
     * 查询默认地址
     */
    @GetMapping("/default")
    public R selectDefault(HttpServletRequest request){
        Long userId = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId,userId).eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(wrapper);
        return R.success(one);
    }




    /**
     * 根据userId查询所有地址
     */

    @GetMapping("/list")
    public R selectAll(HttpServletRequest request){
        Long userId = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId,userId);
        List<AddressBook> list = addressBookService.list(wrapper);
        return R.success(list);
    }

    /**
     * 删除地址【可能是多个】
     */
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        addressBookService.removeByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 修改地址
     */
    @PutMapping
    public R update(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

}
